<template>
  <!-- 角色列表 -->
  <div class="roles-container">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/roles' }">角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 显示权限对话框 -->
    <el-dialog title="提示" :visible.sync="setRightDiaolog" width="30%" center @close="setDislogClose()">
      <el-tree :data="setRightsList" :props="treeProps" show-checkbox node-key="id" default-expand-all
        :default-checked-keys="treeId" ref="treeRights">
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="setRightDiaolog = false">取 消</el-button>
        <el-button type="primary" @click="addSetRights">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 卡片区域 -->
    <el-card shadow="always">
      <el-row> <el-button type="primary">添加角色</el-button></el-row>
      <el-table stripe style="width: 100%" :data="rolesList" border>
        <!-- 展开列 -->
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row class="flex-container" v-for="(item, index1) in scope.row.children" :key="item.id"
              :class="['bdBottom', index1 == 0 ? 'bdTop' : '']">
              <!-- 第一 -->
              <el-col :span="4"><el-tag closable @close="handleClose(scope.row, item)">{{ item.authName }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 第二第三 -->
              <el-col :span="20">
                <el-row v-for="(itemChildren, index2) in item.children" :key="itemChildren.id"
                  :class="[index2 == item.children.length - 1 ? '' : 'bdBottom']">
                  <!-- 二级权限 -->
                  <el-col :span="6">
                    <el-tag type="success" closable @close="handleClose(scope.row, itemChildren)">{{ itemChildren.authName
                    }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <!-- 三级级权限 -->
                  <el-col :span="18">
                    <el-row>
                      <el-col>
                        <el-tag closable type="warning" v-for="(itemTree) in itemChildren.children" :key="itemTree.id"
                          @close="handleClose(scope.row, itemTree)">{{
                            itemTree.authName }}</el-tag>

                      </el-col>
                    </el-row>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index" label="#" align="center">
        </el-table-column>
        <el-table-column prop="roleName" label="角色名称" align="center">
        </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述" align="center">
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改该角色资料" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="给角色提高权限" placement="top" :enterable="false">
              <el-button type="warning" icon="el-icon-help" circle @click="showSetRights(scope.row)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除该角色" placement="top" :enterable="false">
              <el-button type="danger" icon="el-icon-delete" circle></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      rolesList: [], // 角色数据
      setRightDiaolog: false, // 展示权限对话框
      setRightsList: [], // 所有权限
      treeProps: {
        label: 'authName',
        children: 'children'
      },
      treeId: [], // 三级权限ID
      roleId: '' // 角色id
    }
  },
  created() {
    // 获取角色数据
    this.getRoles()
  },
  methods: {
    // 获取角色数据
    async getRoles() {
      const { data: res } = await this.$http.get('/roles')
      if (res.meta.status !== 200) return this.$message.error('获取失败')
      this.rolesList = res.data
    },
    // 三删除三级权限
    async handleClose(role, children) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(error => error)
      // 判断用户点击了确认
      if (confirmResult !== 'confirm') return this.$message.warning('用户取消了删除')
      // 如果点击确认就删除
      const { data: res } = await this.$http.delete(`/roles/${role.id}/rights/${children.id}`)
      // 判断是否成功取消权限
      if (res.meta.status !== 200) return this.$message.error('取消权限失败')
      role.children = res.data
      return this.$message.success('取消权限成功')
    },
    // 展示用户权限对话框
    async showSetRights(role) {
      this.roleId = role.id
      this.setRightDiaolog = true
      // 获取所有权限列表
      const { data: res } = await this.$http.get('/rights/tree')
      if (res.meta.status !== 200) return this.$message.error('获取权限失败')
      this.setRightsList = res.data
      this.getLeafKeys(role, this.treeId)
    },
    // 递归获取三级节点
    getLeafKeys(node, arr) {
      // 判断是不是三级节点
      if (!node.children) return arr.push(node.id)
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    },
    setDislogClose() {
      this.treeId = []
    },
    // 权限配置
    async addSetRights() {
      const keys = [
        ...this.$refs.treeRights.getHalfCheckedKeys(),
        ...this.$refs.treeRights.getCheckedKeys()
      ]
      const idStr = keys.join(',')
      const { data: res } = await this.$http.post(`/roles/${this.roleId}/rights`, { rids: idStr })
      if (res.meta.status !== 200) return this.$message.error('更新权限失败')
      this.getRoles()
      this.roleId = ''
      this.setRightDiaolog = false
      return this.$message.success('更新权限成功')
    }
  }
}
</script>

<style lang="less" scoped>
.roles-container {
  .el-breadcrumb {
    margin-bottom: 10px;
  }

  .bdTop {
    border-top: 1px solid rgba(0, 0, 0, .1);
  }

  .bdBottom {
    border-bottom: 1px solid rgba(0, 0, 0, .1);
  }

  .el-tag {
    margin: 0 10px 5px 0;
  }
}
</style>
